package com.zjitc.utils.old;/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/4/5 22:51
 * @description:
 */

import com.zjitc.common.Gsonal;
import com.zjitc.common.redis.RedisCache;

import com.zjitc.mapper.*;
import com.zjitc.pojo.*;
import com.zjitc.vo.product.Inventory;

import java.time.LocalDateTime;
import java.util.List;

import static com.alibaba.druid.sql.parser.Token.ORDER;

/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/4/5 22:51
 * @description:
 */
@SuppressWarnings("ALL")
public class RedisAuto {

  private static final Integer DAY = 24 * 60 * 60;
  private static final Integer HOUR = 60 * 60;
  private static LocalDateTime localDateTime;

  /**
   * 所有用户的缓存
   * @param dao
   * @param redis
   * @param name
   */
  public static void usersRedis(UserMapper dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<User> users = dao.findAll();
    users.forEach(item -> {
      String tojson = Gsonal.tojson(item);
      redis.hashset(name, item.getUsername(), tojson);
    });
    redis.expire(name, HOUR);
  }

  /**
   * 商品分类的缓存
   * @param dao
   * @param redis
   * @param name
   */
  public static void categoryRedis(CategoryMapper dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Category> categories = dao.findAll();
    categories.forEach(item -> {
      String tojson = Gsonal.tojson(item);
      redis.hashset(name, item.getCid(), tojson);
    });
    redis.expire(name, DAY);
  }

  /**
   * 所有商品的缓存
   * @param dao
   * @param cdao
   * @param redis
   * @param name
   */
  public static void productRedis(ProductMapper dao, CategoryMapper cdao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> findall = dao.findall();
    findall.forEach(product -> {
      Category byCid = cdao.findbycid(product.getCid());
      product.setCategory(byCid);
      String tojson = Gsonal.tojson(product);
      redis.hashset(name, product.getPid(), tojson);
    });
    redis.expire(name, DAY);

    productBycidRedis(dao,cdao,redis);
  }

  /**
   * 每个种类对应商品的缓存
   * @param dao
   * @param cdao
   * @param redis
   */
  private static void productBycidRedis(ProductMapper dao, CategoryMapper cdao, RedisCache redis) {
    List<Category> list = cdao.findAll();
    String temp = "category";
    list.forEach(category -> {
      if(redis.exists(temp + category.getCid())){
        redis.del(temp + category.getCid());
      }
      List<Product> bycid = dao.findBycid(category.getCid());
      bycid.forEach(product -> {
        product.setCategory(category);
        String tojson = Gsonal.tojson(product);
        redis.hashset(temp + category.getCid(),product.getPid(),tojson);
      });
      redis.expire(temp+category.getCid(),DAY);
    });
  }
  /**
   * 热销商品的缓存
   * @param dao
   * @param redis
   * @param name
   */
  public static void hotsRedis(ProductMapper dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> hots = dao.findHots();
    hots.forEach(product -> {
      String tojson = Gsonal.tojson(product);
      redis.hashset(name, product.getPid(), tojson);
    });
    redis.expire(name, DAY);
  }
  /**
   * 最新商品的缓存
   * @param dao
   * @param redis
   * @param name
   */
  public static void newsRedis(ProductMapper dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> news = dao.findNews();
    news.forEach(product -> {
      String tojson = Gsonal.tojson(product);
      redis.hashset(name, product.getPid(), tojson);
    });
    redis.expire(name, DAY);
  }

  /**
   * 前台用到的订单缓存
   * @param dao
   * @param pdao
   * @param redis
   * @param name
   */
  public static void orderRedis(OrderMapper dao, ProductMapper pdao, RedisCache redis, String uid) {
    String name = "order" + uid.substring(0,10);
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Orders> orders = dao.findOrderbyuid(uid);
    orders.forEach(item -> {
      List<Orderitem> listitem = dao.findOrderitem(item.getOid());
      listitem.forEach(orderitems -> {
        Product p = pdao.findbypid(orderitems.getPid());
        orderitems.setSubtotal(p.getShelfPrice() * orderitems.getCount());
        orderitems.setProduct(p);
      });

      item.setOrderitems(listitem);

      String tojson = Gsonal.tojson(item);
      redis.hashset(name, item.getOid(), tojson);
    });
    redis.expire(name, DAY);

  }

  public static void orderRedisAll(OrderMapper dao, ProductMapper pdao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Orders> ordersList = dao.findAll();
    ordersList.forEach(orders -> {
      List<Orderitem> listitem = dao.findOrderitem(orders.getOid());
      listitem.forEach(orderitems -> {
        Product p = pdao.findbypid(orderitems.getPid());
        orderitems.setSubtotal(p.getShelfPrice() * orderitems.getCount());
        orderitems.setProduct(p);
      });
      orders.setOrderitems(listitem);
      String tojson = Gsonal.tojson(orders);
      redis.hashset(name, orders.getOid(), tojson);
    });
    redis.expire(name, DAY);
  }

  public static void inventoryRedis(ProductMapper productMapper, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> products = productMapper.findall();
    products.forEach(product -> {
      redis.hashset(name, product.getPid(), Gsonal.tojson(product.getStock()));
    });

  }

  public static void collectRedis(CollectMapper collectMapper, RedisCache redis, String uid, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Collect> collectList = collectMapper.findCollectbyuid(uid);
    collectList.forEach(collect -> {
      redis.hashset(name,collect.getPid(), Gsonal.tojson(collect));
    });
  }
}
